<?php
    #-------------------------------------------------------------#
	# This file is part of the Kumomado - Virtual Window Platform #
	# for PHP. (c) 2009 Tsai, Min Che <tirme0812@gmail.com>       #
	# Please visit http://www.kumomado.org/ for more information. #
	#                                                             #
	# This software is licensed under the GNU License v3. Please  #
	# view the LICENSE file for the full text of this license.    #
    #-------------------------------------------------------------#

	Kumomado::load('MVCHelper.KumomadoAbstractMVCHelper');
	/**
	 * @package libraries/application/
	 * @author Tsai, Min Che
	 * @version 1.0
	 */
	class KumomadoApplication extends KumomadoAbstractMVCHelper {
		public $app;
		public $sub_app;
		public $app_name;
		public $app_path;
		public $method;
		/**
		 * Construct
		 * @param string $app
		 * @param string $sub_app
		 */
		public function __construct($app, $sub_app = '', $method = 'main') {
			$this->app_path = KUMOMADO_APPLICATIONS_PATH.$app.DIRECTORY_SEPARATOR;
			$this->app = ucfirst(strtolower($app));
			$this->app_name = $this->app;
			$this->method = Kumomado::config('main')->ActionPrefix.$method;
			if ($sub_app != '') {
				$this->app_path .= $sub_app.DIRECTORY_SEPARATOR;
				$this->app_name .= $this->sub_app = ucfirst(strtolower($sub_app));
			}
			define('KUMOMADO_APPLICATION_APP', $this->app);
			define('KUMOMADO_APPLICATION_SUB_APP', $this->sub_app);
			define('KUMOMADO_APPLICATION_NAME', $this->app_name);
			define('KUMOMADO_APPLICATION_PATH', $this->app_path);
			define('KUMOMADO_APPLICATION_LAYOUTS_PATH', KUMOMADO_APPLICATION_PATH.'layouts'.DIRECTORY_SEPARATOR);
			define('KUMOMADO_APPLICATION_URL', 'application.php?app='.$this->app.'&sub_app='.$this->sub_app);
			define('KUMOMADO_APPLICATION_REQUEST', 'application.php?app='.$this->app.'&sub_app='.$this->sub_app.'&method='.$this->method);
			define('KUMOMADO_APPLICATION_REAL_URL', KUMOMADO_APPLICATIONS_URL.$this->app.'/'.(isset($this->sub_app) ? $this->sub_app.'/' : ''));

			define('KUMOMADO_LANGUAGES_PATH', KUMOMADO_APPLICATION_PATH.'languages'.DIRECTORY_SEPARATOR);

			$this->init($this->loadController(), $this->loadModel(), $this->loadView());
		}
		public function loadModel() {
			$model_name = $this->app_name.'Model';
			$model_file = $this->app_path.'model'.DIRECTORY_SEPARATOR.$model_name.'.class.php';
			if (file_exists($model_file)) {
				include_once $model_file;
				return new $model_name;
			} else {
				Kumomado::load('Application.Model.KumomadoApplicationModel');
				return new KumomadoApplicationModel;
			}
		}
		public function loadController() {
			$controller_name = $this->app_name.'Controller';
			$controller_file = $this->app_path.'controller'.DIRECTORY_SEPARATOR.$controller_name.'.class.php';
			if (file_exists($controller_file)) {
				include_once $controller_file;
				return new $controller_name;
			} else {
				throw new KumomadoApplicationException('The application controller file cloud not loaded. ('.$controller_file.')');
			}
		}
		public function loadView() {
			$view_name = $this->app_name.'View';
			$view_file = $this->app_path.'view'.DIRECTORY_SEPARATOR.$view_name.'.class.php';
			if (file_exists($view_file)) {
				include_once $view_file;
				return new $view_name;
			} else {
				Kumomado::load('Application.View.KumomadoApplicationView');
				return new KumomadoApplicationView;
			}
		}
		public static function getMethodURL($method) {
			$url = '';
			if (method_exists(KUMOMADO_APPLICATION_NAME.'Controller', $method)) {
				$url = KUMOMADO_APPLICATION_URL.'&method='.$method;
			}
			return $url;
		}
	}
?>